<?php

namespace app\modules\api\controllers;

use app\controllers\BaseController;
use app\models\CommonModel;
use app\models\LoginForm;
use app\models\tableModel\AdminUserModel;

/**
 * [公共操作]操作控制器
 */
class IndexController extends BaseController
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'rules' => [
                    [ // 必须登陆才能访问
                        'actions' => [
                            'login-detail',
                            'get-reg-code',
                            're-captcha',
                            'register',
                            'login',
                            'logout'
                        ],
                        'allow' => true,
                        'roles' => ['@']
                    ],
                    [ // 无需登陆即可访问
                        'actions' => [
                            'get-reg-code',
                            're-captcha',
                            'register',
                            'login'
                        ],
                        'allow' => true,
                        'roles' => ['?']
                    ],
                ],
            ]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {

        return [
            're-captcha' => [
                'class' => 'app\models\MyCaptchaModel',
                // 'fixedVerifyCode' => substr(md5(time()), 11, 4),
                'backColor' => 0xFFFFFF,//背景颜色
                'maxLength' => 5, //最大显示个数
                'minLength' => 5,//最少显示个数
                'padding' => 0,//间距
                'height' => 40,//高度
                'width' => 105,  //宽度
                'foreColor' => 0x000000,     //字体颜色
                'offset' => 4,        //设置字符偏移量 有效果
                // 'fontFile' =>'' // 规定字体文件路径 example：dirname(Yii::$app->basePath) . '/source/font/elephant.ttf'
                //'controller'=>'login',        //拥有这个动作的controller
            ],
        ];
    }

    /**
     * 登录
     */
    public function actionLogin()
    {

        if (!\Yii::$app->user->isGuest) return $this->jsonSuccess('成功');

        // 登录所需数据
        $data = [
            'LoginForm' => [
                'username' => $this->post('username'),
                'password' => $this->post('password'),
                'rememberMe' => $this->post('rememberMe')
            ]
        ];

        // 实例化登录类
        $model = new LoginForm();
        // 执行登录
        if (!$model->load($data) || !$model->login()) {

            $err = CommonModel::getModelError($model->errors);
            return $this->jsonFail($err['msg'], 400, [
                'errorHint' => $err
            ]);
        }

        // 提示
        return $this->jsonSuccess('成功');
    }

    /**
     * 获取登陆账号信息
     */
    public function actionLoginDetail()
    {

        // 只能获取已登陆自己的详情
        $id = \Yii::$app->getUser()->id;
        if (empty($id)) {

            return $this->jsonFail('请先登陆', 403, [
                'error_detail' => '请先登陆',
                'error_code' => 403
            ]);
        }

        $detail = AdminUserModel::loadModel($id);

        // 看获取的data是否为空
        if (empty($detail)) {

            // 强制退出下
            $this->actionLogout();
            return $this->jsonFail('会话已过期，请重新登录', 403, [
                'error_detail' => '用户不存在',
                'error_code' => 404
            ]);
        }

        // 账号是否禁用
        if ($detail['status'] == AdminUserModel::getStatDisabled()) {

            return $this->jsonFail('您的账号已经禁止使用，请联系管理员', 400, [
                'error_detail' => '账号已被封禁',
                'error_code' => 404
            ]);
        }

        $detail = $detail->getAttributes([
            'id',
            'user_name',
            'nick_name',
            'status',
            'avatar',
            'role_id'
        ]);

        return $this->jsonSuccess('成功', [
            'userInfo' => $detail
        ]);
    }

    /**
     * 退出登录
     */
    public function actionLogout()
    {

        \Yii::$app->user->logout();

        $userInfo = \Yii::$app->getUser()->identity;
        if ($userInfo) {
            return $this->jsonFail('退出登录失败', 400);
        }

        return $this->jsonSuccess('成功');
    }
}
